package com.sdvdxl.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import javax.annotation.Resource;

import java.sql.PreparedStatement;

public class DBHandler<T>  implements DB<T>{
	private Connection conn;
	@SuppressWarnings("unused")
	private ResultSet rs;
	private PreparedStatement pstmt;
	
	public Connection getConn() {
		return conn;
	}
	
	@Resource(name="dataSource")
	public void setConn(Connection conn) {
		this.conn = conn;
	}

	@Override
	public void prepareSql(String sql) throws SQLException {
		if (pstmt==null) {
			pstmt = conn.prepareStatement(sql);
		}
	}

	@Override
	public int execute() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setString(int index, String value) throws SQLException {
		pstmt.setString(index, value);
	}

	@Override
	public void setDouble(int index, Double value) throws SQLException {
		pstmt.setDouble(index, value);
	}

	@Override
	public void setFloat(int index, Float value) throws SQLException {
		pstmt.setFloat(index, value);
	}

	@Override
	public void setDate(int index, Date value) throws SQLException {
		pstmt.setDate(index, new java.sql.Date(value.getTime()));
	}

	@Override
	public void setLong(int index, Long value) throws SQLException {
		pstmt.setLong(index, value);
	}

	@Override
	public void setObject(int index, Object value) throws SQLException {
		pstmt.setObject(index, value);
	}

	@Override
	public void setTimestamp(int index, Timestamp value) throws SQLException {
		pstmt.setTimestamp(index, value);
	}

	@Override
	public ResultSet executeQuery() throws SQLException {
		return pstmt.executeQuery();
	}
}
